package wcg;

import ikelib.FileDropTarget;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


/**
 * ファイルをドロップできるJTable
 * @author ikechan
 *
 */
public class FileDropTable extends JTable implements MouseListener{

	
	JPopupMenu PopupMenu = new JPopupMenu();
	/**
	 * 
	 */
	public FileDropTable(){
		addMouseListener(this);
		setInitialData();
		
		
		addPopupMenuItem(PopupMenu,"Remove All", new ActionListener(){ 
			public void actionPerformed(ActionEvent e) {
				RemoveAll();
			}
	    },new ImageIcon(getClass().getClassLoader().getResource("images/hideall.png")));		
		addPopupMenuItem(PopupMenu,"Remove Selected", new ActionListener(){ 
			public void actionPerformed(ActionEvent e) {
				RemoveSelected();
			}
	    },new ImageIcon(getClass().getClassLoader().getResource("images/hide.png")));		

	}
	/**
	 * POPUPメニューを追加する
	 * @param popup
	 * @param name
	 * @param al
	 * @return
	 */
	private JMenuItem addPopupMenuItem(JPopupMenu popup,String name, ActionListener al) {
		JMenuItem item = new JMenuItem(name);
		item.addActionListener(al);
		popup.add(item);
		return item;
	}
	/**
	 * POPUPメニューを追加する
	 * @param popup
	 * @param name
	 * @param al
	 * @return
	 */
	private JMenuItem addPopupMenuItem(JMenu popup,String name, ActionListener al) {
		JMenuItem item = new JMenuItem(name);
		item.addActionListener(al);
		popup.add(item);
		return item;
	}	private JMenuItem addPopupMenuItem(JPopupMenu popup,String name, ActionListener al,ImageIcon icon) {
		JMenuItem item = addPopupMenuItem(popup,name,al);//@
		item.setIcon(icon);
		return item;
	}
	
	/**
	 * 
	 * @author ikechan
	 *
	 */
	public class MyTableModel extends DefaultTableModel{
	    MyTableModel(String[] columnNames, int rowNum){
	        super(columnNames, rowNum);
	    }
	    public MyTableModel(Object[][] dat, String[] col) {
	    	super(dat, col);
		}
		public Class getColumnClass(int col){
			int colUse = getColumn("use").getModelIndex();
			if(col==colUse){
				Boolean b = new Boolean(true);
				return b.getClass();
			}
			String str = new String();
			return str.getClass();
	    }
	}
	/**
	 * 最初にセットしておくデータをセット（ないとドロップできない）
	 */
	private void setInitialData(){
		String[] col = { "No","use","Input File"};
		Object[][] dat = new Object[1][3];
		dat[0][0] =  "";
		dat[0][2] = "Drop file here";
		dat[0][1] = new Boolean(true);
		MyTableModel mdl = new MyTableModel(dat,col);	// @
		this.setModel(mdl);
		TableColumn tc  = this.getColumn("No");
		tc.setMaxWidth(70);;
		TableColumn tc2  = this.getColumn("use");
		tc2.setMaxWidth(50);;
		this.setDropTarget(new FileDropTarget());
		setAutoCreateRowSorter(true);
	}
	/**
	 * 選択されているITEMを削除
	 */
	private void RemoveSelected(){
		DefaultTableModel mdl = (DefaultTableModel)getModel();
		for(int i=0;i<getRowCount();i++){
			if(isRowSelected(i)){
				int n = this.convertRowIndexToModel(i);
				mdl.removeRow(n);
				i--;
			}
		}
		if(getRowCount()==0){
			setInitialData();
		}
	}
	/**
	 * すべてのITEMを削除
	 */
	private void RemoveAll(){
		DefaultTableModel mdl = (DefaultTableModel)getModel();
		for(int i=0;i<getRowCount();i++){
			int n = this.convertRowIndexToModel(i);
			mdl.removeRow(n);
			i--;
		}
		setInitialData();
	}
	/**
	 * 指定のITEMを削除
	 */
	public void RemoveItem(String item){
		DefaultTableModel mdl = (DefaultTableModel)getModel();
		for(int i=0;i<getRowCount();i++){
			int n = this.convertRowIndexToModel(i);
			String filename = (String)mdl.getValueAt(n, 2);
			if(filename.compareToIgnoreCase(item)==0){
				mdl.removeRow(n);
				i--;
			}
		}
		setInitialData();
	}
	
	/**
	 * ファイル名を返す
	 * @return
	 */
	public Vector<String> getFileNames(){
		Vector<String> ret = new Vector<String>();

		int colFile = getColumn("Input File").getModelIndex();
		int colUse = getColumn("use").getModelIndex();

		for(int i=0;i<getRowCount();i++){
			String f = (String)this.getValueAt(i,colFile);
			if((Boolean)this.getValueAt(i,colUse)){
				if(f!=null){
					if(!f.isEmpty()){
						ret.add(f);
					}
				}
			}
		}
		return ret;
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		int mod = e.getModifiers();
		boolean ctrlKey = (ActionEvent.CTRL_MASK & mod)!=0;
		boolean shiftKey = (ActionEvent.SHIFT_MASK & mod)!=0;
		if (SwingUtilities.isRightMouseButton(e)) {
			PopupMenu.show(e.getComponent(), e.getX(), e.getY());
		}
		else if(SwingUtilities.isLeftMouseButton(e)){
		}
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		
	}
}
